<include file="public@header"/>
</head>
<body>
<div class="wrap ">
    <ul class="nav nav-tabs">
        <li><a href="{:url('Rbac/authorize',['id'=>$role_id])}">后台界面授权</a></li>
        <li class="active"><a href="{:url('Rbac/apiAuthorize',['id'=>$role_id])}">后台API授权</a></li>
    </ul>
    <form class="js-ajax-form margin-top-20" action="{:url('Rbac/apiAuthorizePost')}" method="post">
        <div><a href="javascript:openApiImportDialog()" class="btn btn-xs btn-primary">导入API</a></div>
        <div class="panel-group margin-top-10" id="accordion" role="tablist" aria-multiselectable="true">
            <foreach name="tags_admin_apis" item="tag_admin_apis" key="tag" index="tags_admin_apis_index">
                <div class="panel panel-default">
                    <div class="panel-heading" role="tab" id="heading{$tags_admin_apis_index}">
                        <h4 class="panel-title">
                            <a role="button" data-toggle="collapse" data-parent="#accordion1"
                               href="#collapse{$tags_admin_apis_index}" aria-expanded="true"
                               aria-controls="collapse{$tags_admin_apis_index}">
                                {$tag|default='未分组'}
                            </a>
                        </h4>
                    </div>
                    <div id="collapse{$tags_admin_apis_index}" class="panel-collapse collapse in" role="tabpanel"
                         aria-labelledby="heading{$tags_admin_apis_index}">
                        <table class="table js-check-wrap table-hover" style="table-layout: fixed;">
                            <thead>
                            <tr>
                                <th width="30"><input type="checkbox" class="js-check-all" data-direction="x"
                                                      data-checklist="js-check-x{$tags_admin_apis_index}"></th>
                                <th>名称</th>
                                <th>API</th>
                                <th>备注</th>
                            </tr>
                            </thead>
                            <foreach name="tag_admin_apis" item="admin_api">
                                <tr>
                                    <td><input type="checkbox" class="js-check" data-yid="js-check-y"
                                               data-xid="js-check-x{$tags_admin_apis_index}" name="ids[]"
                                               value="{$admin_api.id}" {$admin_api._checked?'checked':''}>
                                    </td>
                                    <td>{$admin_api.name}</td>
                                    <td>{$admin_api.url}</td>
                                    <td>{$admin_api.remark}</td>
                                </tr>
                            </foreach>
                        </table>
                    </div>
                </div>
            </foreach>
        </div>
        <div class="form-actions text-center">
            <input type="hidden" name="role_id" value="{$role_id}"/>
            <button class="btn btn-primary js-ajax-submit" type="submit">{:lang('SAVE')}</button>
        </div>
    </form>
</div>
<script src="__STATIC__/js/admin.js?v={$_static_version}"></script>
<script type="text/javascript">
    $(document).ready(function () {
        Wind.css('treeTable');
        Wind.use('treeTable', function () {
            $("#authrule-tree").treeTable({
                indent: 20
            });
        });
    });

    function openApiImportDialog() {
        parent.openIframeLayer("{:url('Api/import')}", '导入API', {
            end: function () {
                window.location.reload();
            }
        });
    }

    $('.js-check-wrap').each(function () {
        $(this).find('.js-check:eq(0)').change();
    });

    function checknode(obj) {
        var chk = $("input[type='checkbox']");
        var count = chk.length;

        var num = chk.index(obj);
        var level_top = level_bottom = chk.eq(num).attr('level');
        for (var i = num; i >= 0; i--) {
            var le = chk.eq(i).attr('level');
            if (le < level_top) {
                chk.eq(i).prop("checked", true);
                var level_top = level_top - 1;
            }
        }
        for (var j = num + 1; j < count; j++) {
            var le = chk.eq(j).attr('level');
            if (chk.eq(num).prop("checked")) {

                if (le > level_bottom) {
                    chk.eq(j).prop("checked", true);
                } else if (le == level_bottom) {
                    break;
                }
            } else {
                if (le > level_bottom) {
                    chk.eq(j).prop("checked", false);
                } else if (le == level_bottom) {
                    break;
                }
            }
        }
    }
</script>
</body>
</html>
